<template>
  <div>
    <el-table v-loading="loading" :data="noticeList">
      <!-- <el-table-column label="序号" align="center" prop="order" width="50" /> -->
      <!-- <el-table-column label="图片" align="center" min-width="120">
        <template slot-scope="scope">
          <el-image fit="cover" style="width:100px;height:100px" :src="scope.row.image"></el-image>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="计划编号" align="center" min-width="150">
        <template slot-scope="scope">
          <el-link @click="handledetailed(scope.row)" type="primary">{{ scope.row.planNum || '-' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="任务编号" align="center" min-width="150">
        <template slot-scope="scope">
          <el-link @click="handledetailed(scope.row)" type="primary">{{ scope.row.taskNum || '-' }}</el-link>
        </template>
      </el-table-column> -->
      <el-table-column label="广告名称" align="center" min-width="150">
        <template slot-scope="scope">
          {{ scope.row.advName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="功能模块" align="center" min-width="130">
        <template slot-scope="scope">
          {{ ['首页', '工作台'][scope.row.model] || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="广告位置" align="center" min-width="120">
        <template slot-scope="scope">
          {{ ['首页轮播图'][scope.row.place] || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="链接类型" align="center" min-width="120">
        <template slot-scope="scope">
          {{ ['无链接'][scope.row.linkType] || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="链接结果" align="center" min-width="120">
        <template slot-scope="scope">
          {{ scope.row.linkResult || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" min-width="150">
        <template slot-scope="scope">
          {{ scope.row.sort || '-' }}
          <!-- <dict-tag :options="statusAllocation" :value="scope.row.status" /> -->
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" min-width="130">
        <template slot-scope="scope">
          <dict-tag :options="statusSystem" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建部门" align="center" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.deptName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.createBy || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="360" fixed="right" align="center">
        <template slot-scope="scope">
          <el-popover :ref="`morePop${scope.$index}`" placement="top" width="160" style="margin-right:10px;">
            <el-form :model="form" :rules="rules" ref="ruleForm" style="margin-top: 10px;">
              <el-form-item label="" prop="sort">
                <el-input-number v-model="form.sort" controls-position="right" :min="1">
                </el-input-number>
              </el-form-item>
            </el-form>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="clearQuit(scope.$index)">取消</el-button>
              <el-button type="primary" size="mini" @click.native="commitClick(scope.$index)">提交</el-button>
            </div>
            <el-button v-hasPermi="['system_advertise_sort']" slot="reference" size="small" type="primary"
              @click="() => form = JSON.parse(JSON.stringify(scope.row))">设置排序
            </el-button>
          </el-popover>
          <el-button v-hasPermi="['system_advertise_update']" size="small" type="primary" icon="el-icon-edit"
            @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-hasPermi="['system_advertise_delete']" size="small" type="danger" icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>

          <el-button v-hasPermi="['system_advertise_img']" size="small" type="primary" icon="el-icon-view"
            @click="handleSelectImage(scope.row.img)">图片
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <template v-if="ImageOpen">
      <DialogImage :src="ImageSrc"></DialogImage>
    </template>
    <div class="card-div">
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <!-- 编辑广告 -->
    <template v-if="open">
      <CreatedAdv :title="title" :rowObj="rowObj"></CreatedAdv>
    </template>
  </div>
</template>

<script>
import { getsystemList, deleteAdv, putChangeSort } from "@/api/system/index.js";
export default {
  // dicts: ['sys_notice_status', 'sys_notice_type'],
  components: {
    CreatedAdv: () => import('../form/CreatedAdv.vue')//审核流程
  },
  data () {
    return {
      ImageOpen: false,
      ImageSrc: '',
      // 遮罩层
      loading: true,
      opendetailed: false,
      detailedObj: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      dictdatalist: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层表格数据
      noticeList: [],
      rowObj: {},
      // 弹出层标题
      title: "修改广告",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'advId',
        isAsc: 'desc'//倒叙
      },
      // 表单参数
      form: {
        sort: 1,
        advId: undefined
      },
      // 表单校验
      rules: {
        sort: [
          { required: true, message: "必填", trigger: "change" }
        ],
      }
    };
  },
  watch: {
  },
  created () {
    this.getList();
  },
  methods: {
    clearQuit (index) {
      this.$refs[`morePop${index}`].doClose()
    },
    commitClick (index) {
      this.$refs['ruleForm'].validate(async (valid) => {
        if (!valid) return false
        let params = {
          advId: this.form.advId,
          sort: this.form.sort
        }
        let data = await putChangeSort(params)
        if (data.code === 200) {
          this.$message.success('操作成功')
          this.$refs[`morePop${index}`].doClose()
          this.getList()
        }
      });

    },
    handleSelectImage (src) {
      this.ImageOpen = true
      this.ImageSrc = src
    },
    /** 查询列表 */
    async getList (value) {
      this.loading = true;
      let obj = value || {}
      let params = {
        ...this.queryParams,
        ...obj
      }
      getsystemList(params).then(response => {
        this.noticeList = response.data && response.data.map((item, index) => {
          return {
            ...item,
            order: index + 1
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 打开详细弹窗
    handledetailed (row) {
      this.$emit('handleOpen', row)
      // this.$parent.$parent.$parent.$parent.tabIndex = true
      // this.$parent.$parent.$parent.$parent.activeName = '3'
      // this.$parent.$parent.$parent.$parent.rowObj = row
    },
    /** 生成按钮操作 */
    handleSelect (row) {
      this.$confirm(`是否确认生成盘点任务`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        let params = row.planId
        putTaskGen(params).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.getList()
          }
          this.loading = false;
        }).catch(() => { this.loading = false; });
      }).catch(() => { })
    },
    handleUpdate (row) {//编辑
      this.title = '修改广告'
      this.rowObj = row
      this.open = true
      // this.$emit('handleUpdate', row.planId)
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 启用禁用按钮操作 */
    handleDelete (row) {
      this.$confirm(`是否确认删除该广告`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        let params = row.advId
        deleteAdv(params).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.getList()
          }
          this.loading = false;
        }).catch(() => { this.loading = false; });
      }).catch(() => { })
    }
  },
  filters: {
  }
};
</script>
<style lang="scss" scoped>
.red {
  color: red;
}

.el-input-number--medium {
  width: 134px;
}
</style>